package com.yy.jhl.ui.main.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.ImageLoaderUtils;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.common.commonwidget.ViewPagerFixed;
import com.yy.jhl.R;
import com.yy.jhl.ui.manager.adapter.ViewPageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import uk.co.senab.photoview.PhotoView;

/**
 * Created by Ellis.Li on 2017/9/15.
 */

public class PhotosShowActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    ViewPagerFixed viewPage;
    ArrayList<String> urList;
    int position = 0;
    public static void startAction(Context mContext, ArrayList<String> urList, int position) {
        Intent intent = new Intent(mContext, PhotosShowActivity.class);
        intent.putStringArrayListExtra("urList", urList);
        intent.putExtra("position", position);
        mContext.startActivity(intent);
    }
    @Override
    public int getLayoutId() {
        return R.layout.act_photo;
    }

    @Override
    public void initView() {
        initViewPage();
    }
    /**
     * 初始化ViewPage控件
     */
    private void initViewPage(){
        List<View> viewList = new ArrayList<>();
        urList = getIntent().getStringArrayListExtra("urList");
        position = getIntent().getIntExtra("position",0);
        if (urList != null){
            for (int i=0,size = urList.size();i<size;i++){
                PhotoView photoView = new PhotoView(mContext);
                photoView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
//                photoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                viewList.add(photoView);
                ImageLoaderUtils.displayOn(mContext,photoView,urList.get(i));
            }
            viewPage.setAdapter(new ViewPageAdapter(viewList));
            viewPage.setCurrentItem(position);
            showTitle(String.valueOf(position + 1) + "/" + urList.size(), new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    finish();
                }
            });
            viewPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    showTitle(String.valueOf(position + 1) + "/" + urList.size(), new OnNoDoubleClickListener() {
                        @Override
                        protected void onNoDoubleClick(View v) {
                            finish();
                        }
                    });
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }else {
            finish();
        }
    }
}
